$import("com.joypard.platform.security.role.roleusers.RoleUsersDI");
$import("com.joypard.platform.account.user.UserField");
$import("com.joypard.platform.organization.SelectOrgCompoent");

/**
 * 角色管理-分配用户视图
 */
K.defView("com.joypard.platform.security.role.roleusers.AssignUserWinView", {
	roleusersView : null,

	// 初始化
	initialize : function() {

		E.addEvents("assignUserEvent", "getUsersByRoleIdEvent");

		E.addListener("getUsersByRoleIdSuccessEvent", this.onGetUsersByRoleIdSuccessEvent, this);
		E.addListener("assignUsersSuccessEvent", this.onassignUsersSuccessEvent, this);
	},

	/** ------ ------ ------ ------ 界面组件 ------ ------ ------ */

	/**
	 * 定义一个数组装有已存在的用户
	 */
	roleUsers : new Array(),

	/**
	 * 定义角色id变量
	 */
	roleId : null,

	/**
	 * 创建分配用户主面板
	 */
	createAssignUserPanel : function(roleId) {

		var me = this;
		if (roleId) {
			me.roleId = roleId;
		}
		if (!this.assignUserPanel) {
			this.assignUserPanel = new K.Ext.Window({
				title : "分配权限",
				iconCls : "p-edit",
				layout : "border",
				closeAction : 'hide',
				width : 700,
				height : 500,
				items : [ me.createOrgTreePanel(), me.createUserPanel() ],
				buttons : [ {
					text : '保存',
					iconCls : "w-save",
					handler : function() {
						if (me.createUserPanel().getSelectionModel().getCount() > 0) {
							var records = me.createUserPanel().getSelectionModel().getSelections();
							var usersId = new Array();
							$.each(records, function(i, r) {
								usersId.push(r.id);
							});
							var data = {
								roleId : me.roleId,
								usersId : usersId.join(",")
							}
							E.fireEvent("assignUserEvent", data);
						} else {
							K.err("请选择用户");
						}
					}
				}, {
					text : '关闭',
					iconCls : "w-cancel",
					handler : function() {
						me.createUserPanel().getSelectionModel().clearSelections();
						me.assignUserPanel.hide();
					}
				} ],
				listeners : {
					beforeshow : function() {
						E.fireEvent("getUsersByRoleIdEvent", {
							roleId : me.roleId
						});
					},
					show : function() {
						if (me.createOrgTreePanel()) {
							me.createOrgTreePanel().getSelectionModel().select(me.createOrgTreePanel().root);
						}
					}
				}
			});
		}
		return this.assignUserPanel;
	},

	/**
	 * 用户列表数据源
	 */
	createUserStore : function() {
		var me = this;
		if (!this.userStore) {
			this.userStore = new K.Ext.data.JsonStore({
				url : RoleUsersDI.urls.URL_selectUserList,
				fields : UserField.fields,
				root : "parameters.users",
				listeners : {
					"load" : function(s, r, o) {
						if (me.roleUsers) {
							for ( var i = 0; i < me.roleUsers.length; i++) {
								var record = me.roleUsers[i];
								var rnum = me.userStore.find('id', record.id);
								if (rnum != -1) {
									r[rnum].set('IsRead', 1);
								}
							}
						}
					}
				}
			});
		}
		return this.userStore;
	},

	/**
	 * 创建用户列表
	 */
	createUserPanel : function() {
		var me = this;
		if (!this.userPanel) {
			// 人员信息搜索
			me.userGridUserNameTextField = new Ext.form.TextField({
				xtype : "textfield",
				emptyText : "姓名",
				width : "60"
			});
			// 人员手机搜索
			me.userGridMobileTextField = new Ext.form.TextField({
				xtype : "textfield",
				emptyText : "手机",
				width : "100"
			});

			// 搜索按钮
			me.userGridSearchButton = new Ext.Button({
				iconCls : "search",
				text : "查询",
				handler : K.createDelegate(me.onOrgUsersSearch, this)
			});

			// 回车事件
			function setKeyHandler(text) {
				new Ext.KeyMap(text.getEl(), [ {
					key : [ 10, 13 ],
					fn : function() {
						me.onOrgUsersSearch();
					},
					scope : me
				} ]);
			}

			// 绑定回车键
			me.userGridUserNameTextField.on("afterrender", setKeyHandler, me);
			me.userGridMobileTextField.on("afterrender", setKeyHandler, me);

			var sm = new Ext.grid.CheckboxSelectionModel({
				id : 'ckb',
				width : 25,
				renderer : function(v, c, r) {
					if (r.get("IsRead") == 1) {
						return '<div>&#160;<input type="checkbox" checked="true" disabled="true"/></div>';
					} else {
						return '<div class="x-grid3-row-checker">&#160;</div>';
					}
				},
				listeners : {
					beforerowselect : function(s, n, k, r) {
						if (r.get("IsRead") == 1) {
							return false;
						}
					}
				}
			});
			this.userPanel = new Ext.grid.GridPanel({
				title : "用户列表",
				iconCls : "p-tab",
				viewConfig : {
					forceFit : true
				}, // 自动分配列宽
				store : this.createUserStore(),
				loadMask : true,
				margins : "2 2 2 0",
				sm : sm,
				columns : [ sm, {
					header : "用户编码",
					width : 1,
					sortable : true,
					dataIndex : 'userCode'
				}, {
					id : "userNickName",
					header : "用户名",
					width : 1,
					sortable : true,
					dataIndex : 'userNickName'
				}, {
					header : "邮箱",
					width : 2,
					sortable : true,
					dataIndex : 'email'
				} ],
				region : "center",
				split : true,
				tbar : {
					xtype : "toolbar",
					style : "border-top:0px;border-left:0px;",
					items : [ me.userGridUserNameTextField, ' ', me.userGridMobileTextField, '->', me.userGridSearchButton ]
				},
				bbar : {
					xtype : "pagination"
				}
			});
		}
		return this.userPanel;
	},

	/**
	 * 创建机构树
	 */
	createOrgTreePanel : function() {
		var me = this;
		if (!this.orgTreePanel) {
			this.orgTreePanel = SelectOrgCompoent.createOrgTreePanel({
				width : 200,
				margins : "2 0 2 2",
				split : true
			});
			// 监听点击组织机构事件
			this.orgTreePanel.getSelectionModel().on("selectionchange", function(sm, node) {
				me.userPanel.getStore().load({
					params : {
						orgIds : node.id
					}
				});
			});
		}
		return this.orgTreePanel;
	},

	/**
	 * ------ ------ ------ ------ 事件/监听 ------ ------ ------
	 */

	/**
	 * 根据角色id获取用户成功后处理
	 */
	onGetUsersByRoleIdSuccessEvent : function(roleUsers) {
		this.roleUsers = roleUsers;
	},

	/**
	 * 分配用户成后处理
	 */
	onassignUsersSuccessEvent : function(output) {
		var me = this;
		me.roleUsers = output.getParameter("users");
		var node = me.createOrgTreePanel().getSelectionModel().getSelectedNode();
		me.userPanel.getStore().load({
			params : {
				orgIds : node.id
			}
		});
		me.roleUsersView.userGridPanel.getStore().load({
			params : {
				roleIds : me.roleId
			}
		});
		K.msg("设置成功");
	},

	/**
	 * 点击查询
	 */
	onOrgUsersSearch : function() {
		var params = {};
		params.userNickName = this.userGridUserNameTextField.getValue();
		params.mobile = this.userGridMobileTextField.getValue();
		this.userPanel.getStore().load({
			params : params
		});
	}
});